package com.example.spring_redis.exercise;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
public class Demo1 {
    @Autowired
    public StringRedisTemplate redisTemplate ;
    @RequestMapping("/test")
    public String test(){
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForList().leftPush("key1", "111");
        redisTemplate.opsForList().leftPush("key2", "222");
        redisTemplate.opsForList().leftPush("key3", "333");
        Long key3 = redisTemplate.opsForList().leftPush("key3", "333");
        System.out.println(key3);
        System.out.println(redisTemplate.opsForList().rightPop("key"));
        System.out.println(redisTemplate.opsForList().rightPop("key"));
        return "OK";
    }

    @RequestMapping("/testSet")
    public String testSet(){
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForSet().add("rht", "72", "rll", "98");
        Set<String> members = redisTemplate.opsForSet().members("rht");
        System.out.println(members);
        Boolean res1 = redisTemplate.opsForSet().isMember("rht", "72");
        Boolean res2 = redisTemplate.opsForSet().isMember("rht", "7");
        List<String> key = redisTemplate.opsForSet().pop("rht", 2);
        System.out.println(res1 + " " + res2 + " " + key);
        return "OK";
    }

    @RequestMapping("/testHash")
    public String testHash(){
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForHash().put("day", "date", "2024/6/20");
        String string = (String) redisTemplate.opsForHash().get("day", "date");
        Boolean b = redisTemplate.opsForHash().hasKey("day", "date");
        Long delete = redisTemplate.opsForHash().delete("key", "date");
        System.out.println(string + " " + b + " " + delete);
        return "OK";
    }
}
